import dayjs from "dayjs";
import { getPage as getClientPage, searchPage } from "@/api/idp/client";

import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, onMounted } from "vue";

export function useWinPage() {
  const detailsRef = ref();

  const form = reactive<searchPage>({
    pageNum: 1,
    pageSize: 10,
    ip: "",
    hostname: "",
    branchId: ""
  });

  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      minWidth: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "主机名",
      prop: "hostname",
      width: 180,
      align: "left"
    },
    {
      label: "设备ID",
      prop: "id",
      width: 180,
      align: "left"
    },
    {
      label: "主板序列号",
      prop: "mainBoardSerial",
      width: 180,
      align: "left"
    },
    {
      label: "硬盘序列号",
      prop: "hardDiskSerial",
      width: 180,
      align: "left"
    },
    {
      label: "活跃网卡",
      prop: "activeMAC",
      width: 180,
      align: "left"
    },
    {
      label: "ip",
      prop: "ip",
      width: 180,
      align: "left"
    },

    {
      label: "状态",
      prop: "isOverlap",
      minWidth: 100,
      cellRenderer: ({ row, props }) => (
        <el-tag
          size={props.size}
          type={row.isOverlap ? "danger" : "success"}
          effect="plain"
        >
          {row.isOverlap ? "重复" : "正常"}
        </el-tag>
      )
    },
    {
      label: "在线",
      prop: "isOnline",
      minWidth: 100,
      cellRenderer: ({ row, props }) => (
        <el-tag
          size={props.size}
          type={row.isOnline ? "success" : "danger"}
          effect="plain"
        >
          {row.isOnline ? "在线" : "离线"}
        </el-tag>
      )
    },
    {
      label: "登记时间",
      minWidth: 200,
      prop: "createdAt",
      formatter: ({ createdAt }) => {
        return dayjs(Number(createdAt) * 1000).format("YYYY-MM-DD HH:mm:ss");
      }
    },
    {
      label: "操作",
      fixed: "right",
      width: 160,
      slot: "operation"
    }
  ];
  function showDetail(row) {
    console.log(row);
  }
  function handleEdit(row) {
    console.log(row);
    detailsRef.value.showDialog(row);
  }
  async function onSearch() {
    loading.value = true;
    form.pageNum = pagination.pageCount;
    form.pageSize = pagination.pageSize;
    const r = await getClientPage(form);
    dataList.value = r.list;
    pagination.total = r.total;
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }
  onMounted(() => {
    onSearch();
  });
  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }
  return {
    detailsRef,
    form,
    loading,
    columns,
    showDetail,
    handleEdit,
    onSearch,
    resetForm,
    pagination,
    dataList,
    handleSelectionChange
  };
}
